package com.hwn.datax.config;

import com.alibaba.druid.filter.Filter;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wnhuang
 * @date 2024/6/1 0:43
 */
@Configuration
@Slf4j
public class DbConfig {

    @Primary
    @Bean
    public DataSource dataSource() {
        DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(new ArrayList<>());
        return dataSource;
    }

    @ConfigurationProperties("spring.datasource.dynamic.druid")
    @Bean
    public DruidConfig druidConfig() {
        return new DruidConfig();
    }


    @Bean
    @Order(2000)
    public DruidDataSourceCreator druidDataSourceCreator(DruidConfig druid) {
        return new DruidDataSourceCreator(druid, (proxyFilters) -> {
            return (List<Filter>) new ArrayList();
        });
    }

}
